#include "../include/evt_threadpool.h"
#include "../include/evt_dispather.h"
#include <boost/function.hpp>

evt_threadpool::evt_threadpool(evt_dispather*   dispather)
:m_main_dispather(dispather),
m_dsp_index(0)
{
    //ctor
}

evt_threadpool::~evt_threadpool()
{
    if(m_threads.size() > 0 ) m_threads.join_all();
}


x_int32     evt_threadpool::add_threads(x_uint32 number)
{
    boost::mutex::scoped_lock   lock(m_disp_vec_mutex);
    evt_dispather* ptr=new evt_dispather();
    m_dispather_ptrs.push_back(ptr);
    m_threads.add_thread(new boost::thread(boost::bind(&evt_dispather::main_loop,ptr)) );
    return 0;
}

void        evt_threadpool::run()
{
    m_threads.add_thread(new boost::thread(boost::bind(&evt_dispather::main_loop,m_main_dispather) ) );
}
void        evt_threadpool::exit()
{
    for(size_t n=0;n<m_threads.size();n++ )
    {
//
    }
    if(m_threads.size() > 0){
        evt_thread_ptr_vec::itrator
    }
    m_threads.release_all();
}

evt_dispather*  evt_threadpool::get_dispather()
{
    boost::mutex::scoped_lock   lock(m_disp_vec_mutex);
    if(m_dispather_ptrs.size() > 0 ){
        BOOST_ASSERT( m_threads.size() == m_dispather_ptrs.size() + 1);
        if(m_dsp_index >= m_dispather_ptrs.size()) m_dsp_index=0;
        return &*(m_dispather_ptrs.begin() + m_dsp_index++ );
        //return m_dispather_ptrs.begin();
    }else{
        return m_main_dispather;
    }
}
